package com.rulin.asp.business.errorcorrection.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rulin.asp.business.entity.Dictionary;
import com.rulin.asp.business.entity.ErrorQuestionParam;
import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.entity.RepeatQuestion;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesAttach;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuestion;
import com.rulin.asp.business.errorcorrection.service.ErrorCorrectionService;
import com.rulin.asp.business.service.DictionaryService;
import com.rulin.asp.util.Consists;

/**
 * 图书纠错录入相关
 * @author yangsy
 *
 */
@Controller
@RequestMapping("/external/errorCorrection")
public class ErrorCorrectionController {

	@Autowired
	private ErrorCorrectionService errorCorrectionService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	/**
	 * 起草图书纠错页面
	 * @param view
	 * @return
	 */
//	@RequestMapping(value = "/draft", method = RequestMethod.GET)
//	public ModelAndView draft(ModelAndView view){
//		String id = UUID.randomUUID().toString();
//		
//		view.addObject("quesId",id);
//		view.setViewName("redirect:/external/errorCorrection/openQuesCollectionPage.html");
//		return view;
//	}
	
	@RequestMapping(value = "/draft", method = RequestMethod.GET)
	public ModelAndView draft(ModelAndView view){
		view.setViewName("/business/notice/notice");
		return view;
	}
	
	/**
	 * 打开图书纠错起草页面或编辑页面
	 * @param view
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openQuesCollectionPage.html", method = RequestMethod.GET)
	public ModelAndView openQuesCollectionPage(ModelAndView view,String quesId){
		setDiv(view);
		
		ErrorQuestion errorQuestion = errorCorrectionService.getErrorQuestion(quesId);
		if(errorQuestion == null){
			errorQuestion = new ErrorQuestion();
			
			Question question = new Question();
			question.setQuesId(quesId);
			question.setQuesDiffLevel(Consists.QUES_DIFF_LEVEL_1);
			question.setQuesType(Consists.QUES_TYPE_0);
			errorQuestion.setQuestion(question);
			
			ErrorQuesAttach errorQuesAttach = new ErrorQuesAttach();
			errorQuesAttach.setQuesId(quesId);
			errorQuestion.setErrorQuesAttach(errorQuesAttach);
		}
		view.addObject("form",errorQuestion);
		if(!"0".equals(errorQuestion.getQuestion().getQuesType())){
			view.setViewName("redirect:/external/errorCorrection/openReadPage.html");
		}else{
			view.setViewName("/business/errorcorrection/quescollection");
		}
		return view;
	}
	
	
	/**
	 * 保存题目信息
	 * @param question
	 */
	@RequestMapping(value = "/saveQuestion.json", method = RequestMethod.POST)
	public void saveQuesition(ErrorQuestion question){
		errorCorrectionService.saveErrorQues(question);
	}
	
	/**
	 * 打开只读页面
	 * @param view
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openReadPage.html", method = RequestMethod.GET)
	public ModelAndView openReadPage(ModelAndView view,String quesId){
		setDiv(view);
		
		ErrorQuestion errorQuestion = errorCorrectionService.getErrorQuestion(quesId);
		
		List<RepeatQuestion> repeatQuestions = getRepeatQuestion(errorQuestion);
		view.addObject("repeatQuestions", repeatQuestions);
		view.addObject("form",errorQuestion);
		
		view.setViewName("/business/errorcorrection/ques-read");
		return view;
	}
	
	private List<RepeatQuestion> getRepeatQuestion(ErrorQuestion errorQuestion) {
		
		ErrorQuestionParam param = new ErrorQuestionParam();
		param.setBookClass(errorQuestion.getQuestion().getBookClass());
		param.setBookSecClass(errorQuestion.getQuestion().getBookSecClass());
		param.setPageNum(errorQuestion.getQuestion().getPageNum());
		param.setQuesId(errorQuestion.getQuestion().getQuesId());
		List<RepeatQuestion> repeatQuestions = errorCorrectionService.getRepeatQuestions(param);
		return repeatQuestions;
	}
	
	/**
	 * 查询已存在的图书纠错
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/queryRepeatQues.json", method = RequestMethod.GET)
	@ResponseBody
	public List<RepeatQuestion> queryRepeatQues(ErrorQuestionParam param){
		List<RepeatQuestion> repeatQuestions = errorCorrectionService.getRepeatQuestions(param);
		return repeatQuestions;
	}
	
	/**
	 * 重复确认
	 * @param quesId
	 */
	@RequestMapping(value = "/confirmRepeat.json", method = RequestMethod.GET)
	public void queryRepeatQues(String quesId){
		errorCorrectionService.confirmRepeat(quesId);
	}
	
	@RequestMapping(value = "/confirmSubmit.json", method = RequestMethod.GET)
	public void confirmSubmit(String quesId){
		errorCorrectionService.confirmSubmit(quesId);
	}
	
	
	public void setDiv(ModelAndView model){
		model.addObject("quesTypes", dictionaryService.getByModel("ques_type_param"));
		model.addObject("quesDiffLevels", dictionaryService.getByModel("ques_diff_level"));
		
		model.addObject("questionSources", dictionaryService.getByModel("question_source"));
		model.addObject("quesBookClasses", dictionaryService.getByModel("ques_book_class"));
		model.addObject("quesBookClasses1", dictionaryService.getByModel("ques_book_class_1"));
		model.addObject("quesBookClasses2", dictionaryService.getByModel("ques_book_class_2"));
		model.addObject("quesBookClasses3", dictionaryService.getByModel("ques_book_class_3"));
		model.addObject("quesBookClasses4", dictionaryService.getByModel("ques_book_class_4"));
		
	}
	
	@RequestMapping(value = "/getDicsByParam.json", method = RequestMethod.GET)
	@ResponseBody
	public List<Dictionary> getDicsByParam(String model){
		return dictionaryService.getByModel(model);
	}
	
}
